package com.cabrida.livestar.main;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cabrida.livestar.R;
import com.cabrida.livestar.about.AboutUsActivity;
import com.cabrida.livestar.config.Global;
import com.cabrida.livestar.main.FansCallStarLvAdapter.OnCallStarListener;
import com.cabrida.livestar.message.MessageActivity;
import com.cabrida.livestar.model.FansAdvanceListData;
import com.cabrida.livestar.model.FansCallStar;
import com.cabrida.livestar.model.FansLiveListData;
import com.cabrida.livestar.model.FansLivePlayBackListData;
import com.cabrida.livestar.pay.PayActivity;
import com.cabrida.livestar.view.GridViewExtend;
import com.cabrida.livestar.view.ListViewExtend;

/**
 * 粉丝端首页
 * 
 * @author why
 */
public class FansHomePageView extends LinearLayout implements OnClickListener {
	private Context mContext;

	private ImageButton ib_dian;
	private GridViewExtend gridView_live;
	private ListViewExtend listView_liveadvance;
	private ListViewExtend listView_callstar;
	private GridViewExtend gridView_playback;
	
//	private LinearLayout ll_call_star;

	private ImageButton ib_msg;
	private FansHomePageLiveAdapter gvAdapter_live;
	private FansHomePageAdvanceAdapter lvAdapter_advance;
	private FansHomePageLivePlayBackAdapter gvAdapter_playback;
	private FansCallStarLvAdapter gvAdapter_callstar;
	
	private LinearLayout ll_live_more;
	private LinearLayout ll_liveadvance_more;
	private LinearLayout ll_liveadvance_callstar;
	private LinearLayout ll_liveplayback_more;

	private Dialog dialogPay;
	/**
	 * 余额充足，可以支付
	 */
	public static final int PAY_TYPE_0 = 0;
	/**
	 * 余额不足
	 */
	public static final int PAY_TYPE_1 = 1;
	/**
	 * 直播已结束
	 */
	public static final int PAY_TYPE_2 = 2;

	public FansHomePageView(Context context) {
		super(context);
		mContext = context;
		View view = LayoutInflater.from(context).inflate(
				R.layout.view_main_fans_homepage, this);
		Init(view);
	}

	/**
	 * 初始化控件
	 */
	private void Init(View view) {
		ImageButton ib_dian = (ImageButton) view.findViewById(R.id.ib_dian);
		ib_dian.setOnClickListener(this);
		ImageButton ib_search = (ImageButton) view.findViewById(R.id.ib_search);
		ib_search.setOnClickListener(this);
//		ll_call_star = (LinearLayout) view.findViewById(R.id.ll_call_star);
		gridView_live = (GridViewExtend) view.findViewById(R.id.gridView_live);
		listView_liveadvance = (ListViewExtend) view.findViewById(R.id.listView_liveadvance);
		listView_callstar = (ListViewExtend) view.findViewById(R.id.listView_callstar);
		gridView_playback = (GridViewExtend) view.findViewById(R.id.gridView_playback);
		ib_msg = (ImageButton) view.findViewById(R.id.ib_msg);
		ib_msg.setOnClickListener(this);
//		ll_call_star.setOnClickListener(this);
		
		ll_live_more = (LinearLayout) view.findViewById(R.id.ll_live_more);
		ll_liveadvance_more = (LinearLayout) view.findViewById(R.id.ll_liveadvance_more);
		ll_liveplayback_more = (LinearLayout) view.findViewById(R.id.ll_liveplayback_more);
		ll_liveadvance_callstar = (LinearLayout) view.findViewById(R.id.ll_liveadvance_callstar);
		ll_live_more.setOnClickListener(this);
		ll_liveadvance_more.setOnClickListener(this);
		ll_liveadvance_callstar.setOnClickListener(this);
		ll_liveplayback_more.setOnClickListener(this);
		
		gvAdapter_live = new FansHomePageLiveAdapter(mContext);
		gridView_live.setAdapter(gvAdapter_live);
		lvAdapter_advance = new FansHomePageAdvanceAdapter(mContext);
		listView_liveadvance.setAdapter(lvAdapter_advance);
		gvAdapter_playback = new FansHomePageLivePlayBackAdapter(mContext);
		gridView_playback.setAdapter(gvAdapter_playback);
		gvAdapter_callstar = new FansCallStarLvAdapter(mContext);
		listView_callstar.setAdapter(gvAdapter_callstar);
		gvAdapter_callstar.setOnCallStarListener(new OnCallStarListener() {
			
			@Override
			public void onCallStar(String s_id) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setAction(MainActivity.ACTION_HOMEPAGE_CALLSTAR);
				intent.putExtra("s_id", s_id);
				mContext.sendBroadcast(intent);
			}
		});

		Intent intent = new Intent();
		intent.setAction(MainActivity.ACTION_HOMEPAGE_LIST);
		mContext.sendBroadcast(intent);

		gridView_live.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				FansLiveListData data = (FansLiveListData) arg0.getAdapter()
						.getItem(arg2);

				Intent intent = new Intent();
				intent.setAction(MainActivity.ACTION_PAYMENT);
				intent.putExtra("data", data);
				mContext.sendBroadcast(intent);
			}
		});
		
		gridView_playback.setOnItemClickListener(new OnItemClickListener() {
			
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				FansLivePlayBackListData data = (FansLivePlayBackListData) arg0.getAdapter()
				.getItem(arg2);
				
				Intent intent = new Intent();
				intent.setClass(mContext, FansRePlayRoomActivity.class);
				intent.putExtra("channel", data.getL_channel());
				mContext.startActivity(intent);
			}
		});

	}

	/**
	 * 进入购买界面
	 */
	public void gotoPay() {
		Intent intent = new Intent();
		intent.setClass(mContext, PayActivity.class);
		mContext.startActivity(intent);
	}

	/**
	 * 进入直播
	 */
	public void setPayment(FansLiveListData data) {
		Intent intent = new Intent();
		intent.setClass(mContext, FansLiveRoomActivity.class);
		intent.putExtra("data", data);
		mContext.startActivity(intent);
	}

	public void popWindows(FansLiveListData data, int type) {
		showPayDialog(data, type);
	}

	private void showPayDialog(FansLiveListData data, int type) {
		initPayDialogView(data, type);
		if (null != dialogPay && dialogPay.isShowing()) {
			return;
		}
		if (null != dialogPay) {
			/**
			 * 等数据加载完毕，显示对话框
			 */
			Window window = dialogPay.getWindow();
			// 设置显示动画
			window.setWindowAnimations(R.style.main_menu_animstyle);
			WindowManager.LayoutParams wl = window.getAttributes();
			wl.x = 0;
			wl.y = ((Activity) mContext).getWindowManager().getDefaultDisplay()
					.getHeight();
			// 以下这两句是为了保证按钮可以水平满屏
			wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
			wl.height = ViewGroup.LayoutParams.MATCH_PARENT;
			// 设置显示位置
			dialogPay.onWindowAttributesChanged(wl);
			// 设置点击外围解散
			dialogPay.setCanceledOnTouchOutside(true);
			dialogPay.show();
		}
	}

	private void initPayDialogView(final FansLiveListData data,
			final int type) {
		LayoutInflater mInflater = LayoutInflater.from(mContext);
		View view = mInflater.inflate(R.layout.dialog_pay, null);
		TextView tv_text = (TextView) view.findViewById(R.id.tv_text);
		TextView tv_msg = (TextView) view.findViewById(R.id.tv_msg);
		ImageView iv_pay = (ImageView) view.findViewById(R.id.iv_pay);
		LinearLayout ll_pay_num = (LinearLayout) view.findViewById(R.id.ll_pay_num);
		LinearLayout ll_num = (LinearLayout) view.findViewById(R.id.ll_num);
		if (type == PAY_TYPE_0) {
			tv_text.setText("确认观看");
			iv_pay.setVisibility(View.VISIBLE);
			tv_msg.setVisibility(View.GONE);
		}
		if (type == PAY_TYPE_1) {
			tv_text.setText("立即购买");
			iv_pay.setVisibility(View.GONE);
			tv_msg.setVisibility(View.VISIBLE);
			tv_msg.setText("余额不足");
		}
		
		if (type == PAY_TYPE_2) {
			tv_text.setText("确定");
			ll_pay_num.setVisibility(View.GONE);
			ll_num.setVisibility(View.GONE);
			iv_pay.setVisibility(View.GONE);
			tv_msg.setVisibility(View.VISIBLE);
			tv_msg.setText("直播已结束");
		}

		ImageButton ib_close = (ImageButton) view.findViewById(R.id.ib_close);
		ib_close.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (null != dialogPay) {
					dialogPay.dismiss();
				}
			}
		});
		LinearLayout ll_sure = (LinearLayout) view.findViewById(R.id.ll_sure);
		ll_sure.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (null != dialogPay) {
					dialogPay.dismiss();
				}
				//setPayment(channel, GroupId);
				if (type == PAY_TYPE_0) {
					setPayment(data);
				}
				if (type == PAY_TYPE_1) {
					gotoPay();
				}

			}
		});

		dialogPay = new Dialog(mContext, R.style.dialogWindowStyle);
		dialogPay.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialogPay.setContentView(view, new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	}
	
	/**
	 * 主页数据
	 */
	public void setLvData(ArrayList<FansLiveListData> infoList_live,ArrayList<FansAdvanceListData> infoList_advance,
			ArrayList<FansLivePlayBackListData> infoList_playback,ArrayList<FansCallStar> infoList_shout ) {
		
		gvAdapter_live.setData(infoList_live);
		gvAdapter_live.notifyDataSetChanged();
		lvAdapter_advance.setData(infoList_advance);
		lvAdapter_advance.notifyDataSetChanged();
		gvAdapter_playback.setData(infoList_playback);
		gvAdapter_playback.notifyDataSetChanged();
		gvAdapter_callstar.setData(infoList_shout);
		gvAdapter_callstar.notifyDataSetChanged();
	}
	
	/**
	 *预播订阅状态修改
	 */
	public void setAdvanceBook(String follow) {
		ArrayList<FansAdvanceListData> data = lvAdapter_advance.getData();
		for (int i = 0; i < data.size(); i++) {
			if (follow.equals(data.get(i).getL_id())) {
				int ischeck = data.get(i).getIsCheck();
				if (ischeck == 0) {
					ischeck = 1;
				} else {
					ischeck = 0;
				}
				data.get(i).setIsCheck(ischeck);
			}
		}
		lvAdapter_advance.setData(data);
		lvAdapter_advance.notifyDataSetChanged();
	}

	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch (v.getId()) {
		case R.id.ib_dian:
			intent = new Intent();
			intent.setClass(mContext, AboutUsActivity.class);
			mContext.startActivity(intent);
			break;
		case R.id.ib_msg:
			intent = new Intent();
			intent.setClass(mContext, MessageActivity.class);
			intent.putExtra("type", Global.TYPE_FANS);
			mContext.startActivity(intent);
			break;
		case R.id.ll_liveadvance_callstar:
			intent = new Intent();
			intent.setClass(mContext, FansCallStarActivity.class);
			mContext.startActivity(intent);
			break;
		case R.id.ll_live_more:
			intent = new Intent();
			intent.setClass(mContext, FansLiveListActivity.class);
			mContext.startActivity(intent);
			break;
		case R.id.ll_liveadvance_more:
			intent = new Intent();
			intent.setClass(mContext, FansAdvanceListActivity.class);
			mContext.startActivity(intent);
			break;
		case R.id.ll_liveplayback_more:
			intent = new Intent();
			intent.setClass(mContext, FansLivePlayBackListActivity.class);
			mContext.startActivity(intent);
			break;
		case R.id.ib_search:
			intent = new Intent();
			intent.setClass(mContext, SearchActivity.class);
			mContext.startActivity(intent);
			break;
		default:
			break;
		}
	}

}
